export default class Snowfall {
  /**
   * 初始化雪花效果
   * @param {string} selector - Canvas 的选择器
   * @param {Object} options - 配置参数
   * @param {number} options.flakeCount - 雪花数量
   * @param {Array} options.radiusRange - 雪花半径范围 [min, max]
   * @param {Array} options.opacityRange - 雪花透明度范围 [min, max]
   * @param {number} options.speed - 雪花下落速度倍数
   * @param {number} options.swingAmplitude - 雪花摆动幅度，默认垂直下落
   */
  constructor(selector, { 
    flakeCount = 100, 
    radiusRange = [2, 5], 
    opacityRange = [0.5, 1], 
    speed = 1, 
    swingAmplitude = 0 
  } = {}) {
    this.canvas = document.querySelector(selector);
    if (!this.canvas) {
      throw new Error(`Canvas "${selector}" not found.`);
    }
    this.ctx = this.canvas.getContext("2d");

    this.flakeCount = flakeCount;
    this.radiusRange = radiusRange;
    this.opacityRange = opacityRange;
    this.speed = speed;
    this.swingAmplitude = swingAmplitude;

    this.flakes = [];
    this.angle = 0;
    this.animationFrameId = null;

    this.initializeCanvas();
    this.createFlakes();
    this.startAnimation();
  }

  // 设置 canvas 尺寸
  initializeCanvas() {
    this.canvas.width = window.innerWidth;
    this.canvas.height = window.innerHeight;
  
    let resizeTimeout = null;
    const resizeHandler = () => {
      clearTimeout(resizeTimeout);
      resizeTimeout = setTimeout(() => {
        this.canvas.width = window.innerWidth;
        this.canvas.height = window.innerHeight;
        this.createFlakes(); // 重新生成雪花
      }, 200); // 节流延时
    };
  
    window.addEventListener("resize", resizeHandler);
  }

  // 生成雪花
  createFlakes() {
    const { flakeCount, radiusRange, opacityRange } = this;
    const [minRadius, maxRadius] = radiusRange;
    const [minOpacity, maxOpacity] = opacityRange;

    this.flakes = Array.from({ length: flakeCount }, () => ({
      x: Math.random() * this.canvas.width,
      y: Math.random() * this.canvas.height,
      r: Math.random() * (maxRadius - minRadius) + minRadius,
      opacity: Math.random() * (maxOpacity - minOpacity) + minOpacity,
      d: Math.random() + 1, // 下落速度因子
    }));
  }

  // 绘制雪花
  drawFlakes() {
    const { ctx, flakes } = this;
    ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    ctx.fillStyle = "white";

    flakes.forEach(flake => {
      ctx.beginPath();
      ctx.globalAlpha = flake.opacity; // 设置透明度
      ctx.arc(flake.x, flake.y, flake.r, 0, Math.PI * 2, true);
      ctx.fill();
    });

    this.moveFlakes();
  }

  // 移动雪花
  moveFlakes() {
    const { flakes, canvas, speed, swingAmplitude } = this;
    this.angle += 0.01;

    flakes.forEach(flake => {
      // 垂直下落速度
      flake.y += (Math.pow(flake.d, 2) + 1) * speed;
      
      // 左右摆动
      if (swingAmplitude > 0) {
        flake.x += Math.cos(this.angle) * swingAmplitude;
      }

      // 如果雪花超出屏幕底部，重置到顶部
      if (flake.y > canvas.height) {
        flake.y = 0;
        flake.x = Math.random() * canvas.width;
      }
    });
  }

  // 开始动画
  startAnimation() {
    const animate = () => {
      this.drawFlakes();
      this.animationFrameId = requestAnimationFrame(animate);
    };
    this.animationFrameId = requestAnimationFrame(animate);
  }

  // 停止动画
  stopAnimation() {
    if (this.animationFrameId) {
      cancelAnimationFrame(this.animationFrameId);
      this.animationFrameId = null;
    }
  }
}